/*     */ package com.cr.framework.config;
/*     */ 
/*     */ import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.common.utils.security.CipherUtils;
/*     */ import com.cr.common.utils.spring.SpringUtils;
/*     */ import com.cr.framework.shiro.realm.UserRealm;
/*     */ import com.cr.framework.shiro.session.OnlineSessionDAO;
/*     */ import com.cr.framework.shiro.session.OnlineSessionFactory;
/*     */ import com.cr.framework.shiro.web.filter.MyLogoutFilter;
/*     */ import com.cr.framework.shiro.web.filter.captcha.CaptchaValidateFilter;
/*     */ import com.cr.framework.shiro.web.filter.kickout.KickoutSessionFilter;
/*     */ import com.cr.framework.shiro.web.filter.online.OnlineSessionFilter;
/*     */ import com.cr.framework.shiro.web.filter.sync.SyncOnlineSessionFilter;
/*     */ import com.cr.framework.shiro.web.session.OnlineWebSessionManager;
/*     */ import com.cr.framework.shiro.web.session.SpringSessionValidationScheduler;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.Map;
/*     */ import javax.servlet.Filter;
/*     */ import net.sf.ehcache.CacheManager;
/*     */ import org.apache.commons.io.IOUtils;
/*     */
/*     */ import org.apache.shiro.cache.ehcache.EhCacheManager;
/*     */ import org.apache.shiro.codec.Base64;
/*     */ import org.apache.shiro.config.ConfigurationException;
/*     */ import org.apache.shiro.io.ResourceUtils;
/*     */ import org.apache.shiro.mgt.RememberMeManager;
/*     */ import org.apache.shiro.mgt.SecurityManager;
/*     */ import org.apache.shiro.realm.Realm;
/*     */ import org.apache.shiro.session.mgt.SessionFactory;
/*     */ import org.apache.shiro.session.mgt.SessionManager;
/*     */ import org.apache.shiro.session.mgt.SessionValidationScheduler;
/*     */ import org.apache.shiro.session.mgt.eis.SessionDAO;
/*     */ import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
/*     */ import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
/*     */ import org.apache.shiro.web.mgt.CookieRememberMeManager;
/*     */ import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
/*     */ import org.apache.shiro.web.servlet.Cookie;
/*     */ import org.apache.shiro.web.servlet.SimpleCookie;
/*     */ import org.springframework.beans.factory.annotation.Qualifier;
/*     */ import org.springframework.beans.factory.annotation.Value;
/*     */ import org.springframework.context.annotation.Bean;
/*     */ import org.springframework.context.annotation.Configuration;
/*     */ 
/*     */ @Configuration
/*     */ public class ShiroConfig {
/*     */   @Value("${shiro.session.expireTime}")
/*     */   private int expireTime;
/*     */   
/*     */   @Value("${shiro.session.validationInterval}")
/*     */   private int validationInterval;
/*     */   
/*     */   @Value("${shiro.session.maxSession}")
/*     */   private int maxSession;
/*     */   
/*     */   @Value("${shiro.session.kickoutAfter}")
/*     */   private boolean kickoutAfter;
/*     */   
/*     */   @Value("${shiro.user.captchaEnabled}")
/*     */   private boolean captchaEnabled;
/*     */   
/*     */   @Value("${shiro.user.captchaType}")
/*     */   private String captchaType;
/*     */   
/*     */   @Value("${shiro.cookie.domain}")
/*     */   private String domain;
/*     */   
/*     */   @Value("${shiro.cookie.path}")
/*     */   private String path;
/*     */   
/*     */   @Value("${shiro.cookie.httpOnly}")
/*     */   private boolean httpOnly;
/*     */   
/*     */   @Value("${shiro.cookie.maxAge}")
/*     */   private int maxAge;
/*     */   
/*     */   @Value("${shiro.cookie.cipherKey}")
/*     */   private String cipherKey;
/*     */   
/*     */   @Value("${shiro.user.loginUrl}")
/*     */   private String loginUrl;
/*     */   
/*     */   @Value("${shiro.user.unauthorizedUrl}")
/*     */   private String unauthorizedUrl;
/*     */   
/*     */   @Value("${shiro.rememberMe.enabled: false}")
/*     */   private boolean rememberMe;
/*     */   
/*     */   @Bean
/*     */   public EhCacheManager getEhCacheManager() {
/* 138 */     CacheManager cacheManager = CacheManager.getCacheManager("ruoyi");
/* 139 */     EhCacheManager em = new EhCacheManager();
/* 140 */     if (MyStringUtils.isNull(cacheManager)) {
/* 142 */       em.setCacheManager(new CacheManager(getCacheManagerConfigFileInputStream()));
/* 143 */       return em;
/*     */     } 
/* 147 */     em.setCacheManager(cacheManager);
/* 148 */     return em;
/*     */   }
/*     */   
/*     */   protected InputStream getCacheManagerConfigFileInputStream() {
/* 157 */     String configFile = "classpath:ehcache/ehcache-shiro.xml";
/* 158 */     InputStream inputStream = null;
/*     */     try {
/* 161 */       inputStream = ResourceUtils.getInputStreamForPath(configFile);
/* 162 */       byte[] b = IOUtils.toByteArray(inputStream);
/* 163 */       InputStream in = new ByteArrayInputStream(b);
/* 164 */       return in;
/* 166 */     } catch (IOException e) {
/* 168 */       throw new ConfigurationException("Unable to obtain input stream for cacheManagerConfigFile [" + configFile + "]", e);
/*     */     } finally {
/* 173 */       IOUtils.closeQuietly(inputStream);
/*     */     } 
/*     */   }
/*     */   
/*     */   @Bean
/*     */   public UserRealm userRealm(EhCacheManager cacheManager) {
/* 183 */     UserRealm userRealm = new UserRealm();
/* 184 */     userRealm.setAuthorizationCacheName("sys-authCache");
/* 185 */     userRealm.setCacheManager(cacheManager);
/* 186 */     return userRealm;
/*     */   }
/*     */   
/*     */   @Bean
/*     */   public OnlineSessionDAO sessionDAO() {
/* 195 */     OnlineSessionDAO sessionDAO = new OnlineSessionDAO();
/* 196 */     return sessionDAO;
/*     */   }
/*     */   
/*     */   @Bean
/*     */   public OnlineSessionFactory sessionFactory() {
/* 205 */     OnlineSessionFactory sessionFactory = new OnlineSessionFactory();
/* 206 */     return sessionFactory;
/*     */   }
/*     */   
/*     */   @Bean
/*     */   public OnlineWebSessionManager sessionManager() {
/* 215 */     OnlineWebSessionManager manager = new OnlineWebSessionManager();
/* 217 */     manager.setCacheManager(getEhCacheManager());
/* 219 */     manager.setDeleteInvalidSessions(true);
/* 221 */     manager.setGlobalSessionTimeout((this.expireTime * 60 * 1000));
/* 223 */     manager.setSessionIdUrlRewritingEnabled(false);
/* 225 */     manager.setSessionValidationScheduler((SessionValidationScheduler)SpringUtils.getBean(SpringSessionValidationScheduler.class));
/* 227 */     manager.setSessionValidationSchedulerEnabled(true);
/* 229 */     manager.setSessionDAO((SessionDAO)sessionDAO());
/* 231 */     manager.setSessionFactory((SessionFactory)sessionFactory());
/* 232 */     return manager;
/*     */   }
/*     */   
/*     */   @Bean
/*     */   public SecurityManager securityManager(UserRealm userRealm) {
/* 241 */     DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
/* 243 */     securityManager.setRealm((Realm)userRealm);
/* 245 */     securityManager.setRememberMeManager(this.rememberMe ? (RememberMeManager)rememberMeManager() : null);
/* 247 */     securityManager.setCacheManager(getEhCacheManager());
/* 249 */     securityManager.setSessionManager((SessionManager)sessionManager());
/* 250 */     return (SecurityManager)securityManager;
/*     */   }
/*     */   
/*     */   public MyLogoutFilter logoutFilter() {
/* 258 */     MyLogoutFilter logoutFilter = new MyLogoutFilter();
/* 259 */     logoutFilter.setLoginUrl(this.loginUrl);
/* 260 */     return logoutFilter;
/*     */   }
/*     */   
/*     */   @Bean
/*     */   public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
/* 269 */     ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
/* 271 */     shiroFilterFactoryBean.setSecurityManager(securityManager);
/* 273 */     shiroFilterFactoryBean.setLoginUrl(this.loginUrl);
/* 275 */     shiroFilterFactoryBean.setUnauthorizedUrl(this.unauthorizedUrl);
/* 277 */     LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
/* 279 */     filterChainDefinitionMap.put("/favicon.ico**", "anon");
/* 280 */     filterChainDefinitionMap.put("/ruoyi.png**", "anon");
/* 281 */     filterChainDefinitionMap.put("/html/**", "anon");
/* 282 */     filterChainDefinitionMap.put("/css/**", "anon");
/* 283 */     filterChainDefinitionMap.put("/docs/**", "anon");
/* 284 */     filterChainDefinitionMap.put("/fonts/**", "anon");
/* 285 */     filterChainDefinitionMap.put("/img/**", "anon");
/* 286 */     filterChainDefinitionMap.put("/ajax/**", "anon");
/* 287 */     filterChainDefinitionMap.put("/js/**", "anon");
/* 288 */     filterChainDefinitionMap.put("/ruoyi/**", "anon");
/* 289 */     filterChainDefinitionMap.put("/api/**", "anon");
/* 289 */     filterChainDefinitionMap.put("/profile/upload/**", "anon");
/* 289 */     filterChainDefinitionMap.put("/swagger-ui/**", "anon");
                filterChainDefinitionMap.put("/swagger-resources", "anon");
                filterChainDefinitionMap.put("/v2/api-docs", "anon");
                filterChainDefinitionMap.put("/webjars/springfox-swagger-ui/**", "anon");
                filterChainDefinitionMap.put("/configuration/security", "anon");
                filterChainDefinitionMap.put("/configuration/ui", "anon");
/* 290 */     filterChainDefinitionMap.put("/captcha/captchaImage**", "anon");
/* 292 */     filterChainDefinitionMap.put("/logout", "logout");
/* 294 */     filterChainDefinitionMap.put("/login", "anon,captchaValidate");
/* 296 */     filterChainDefinitionMap.put("/register", "anon,captchaValidate");
/* 300 */     Map<String, Filter> filters = new LinkedHashMap<>();
/* 301 */     filters.put("onlineSession", onlineSessionFilter());
/* 302 */     filters.put("syncOnlineSession", syncOnlineSessionFilter());
/* 303 */     filters.put("captchaValidate", captchaValidateFilter());
/* 304 */     filters.put("kickout", kickoutSessionFilter());
/* 306 */     filters.put("logout", logoutFilter());
/* 307 */     shiroFilterFactoryBean.setFilters(filters);
/* 310 */     filterChainDefinitionMap.put("/**", "user,kickout,onlineSession,syncOnlineSession");
/* 311 */     shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
/* 313 */     return shiroFilterFactoryBean;
/*     */   }
/*     */   
/*     */   public OnlineSessionFilter onlineSessionFilter() {
/* 321 */     OnlineSessionFilter onlineSessionFilter = new OnlineSessionFilter();
/* 322 */     onlineSessionFilter.setLoginUrl(this.loginUrl);
/* 323 */     onlineSessionFilter.setOnlineSessionDAO(sessionDAO());
/* 324 */     return onlineSessionFilter;
/*     */   }
/*     */   
/*     */   public SyncOnlineSessionFilter syncOnlineSessionFilter() {
/* 332 */     SyncOnlineSessionFilter syncOnlineSessionFilter = new SyncOnlineSessionFilter();
/* 333 */     syncOnlineSessionFilter.setOnlineSessionDAO(sessionDAO());
/* 334 */     return syncOnlineSessionFilter;
/*     */   }
/*     */   
/*     */   public CaptchaValidateFilter captchaValidateFilter() {
/* 342 */     CaptchaValidateFilter captchaValidateFilter = new CaptchaValidateFilter();
/* 343 */     captchaValidateFilter.setCaptchaEnabled(this.captchaEnabled);
/* 344 */     captchaValidateFilter.setCaptchaType(this.captchaType);
/* 345 */     return captchaValidateFilter;
/*     */   }
/*     */   
/*     */   public SimpleCookie rememberMeCookie() {
/* 353 */     SimpleCookie cookie = new SimpleCookie("rememberMe");
/* 354 */     cookie.setDomain(this.domain);
/* 355 */     cookie.setPath(this.path);
/* 356 */     cookie.setHttpOnly(this.httpOnly);
/* 357 */     cookie.setMaxAge(this.maxAge * 24 * 60 * 60);
/* 358 */     return cookie;
/*     */   }
/*     */   
/*     */   public CookieRememberMeManager rememberMeManager() {
/* 366 */     CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
/* 367 */     cookieRememberMeManager.setCookie((Cookie)rememberMeCookie());
/* 368 */     if (MyStringUtils.isNotEmpty(this.cipherKey)) {
/* 370 */       cookieRememberMeManager.setCipherKey(Base64.decode(this.cipherKey));
/*     */     } else {
/* 374 */       cookieRememberMeManager.setCipherKey(CipherUtils.generateNewKey(128, "AES").getEncoded());
/*     */     } 
/* 376 */     return cookieRememberMeManager;
/*     */   }
/*     */   
/*     */   public KickoutSessionFilter kickoutSessionFilter() {
/* 384 */     KickoutSessionFilter kickoutSessionFilter = new KickoutSessionFilter();
/* 385 */     kickoutSessionFilter.setCacheManager(getEhCacheManager());
/* 386 */     kickoutSessionFilter.setSessionManager((SessionManager)sessionManager());
/* 388 */     kickoutSessionFilter.setMaxSession(this.maxSession);
/* 390 */     kickoutSessionFilter.setKickoutAfter(this.kickoutAfter);
/* 392 */     kickoutSessionFilter.setKickoutUrl("/login?kickout=1");
/* 393 */     return kickoutSessionFilter;
/*     */   }
/*     */   
/*     */   @Bean
/*     */   public ShiroDialect shiroDialect() {
/* 402 */     return new ShiroDialect();
/*     */   }
/*     */   
/*     */   @Bean
/*     */   public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager) {
/* 412 */     AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
/* 413 */     authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
/* 414 */     return authorizationAttributeSourceAdvisor;
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-framework-4.7.0.jar!\com\cr\framework\config\ShiroConfig.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */